# delimit ;
clear ;
set more off ;
version 12.0 ;

cd "replication" ;

* ***************************************************************************** ;
* Carpena, Fenella, and Bilal Zia. "The causal mechanism of financial education: 
* Evidence from mediation analysis." Journal of Economic Behavior & Organization 
* 177 (2020): 143-184.
* 
* This do-file creates Appendix Table A6 of the paper
* ***************************************************************************** ;

* ***************************************************************************** ;
* get endline data
* ***************************************************************************** ;

use "./data/endline.dta", clear ;

* merge with baseline ;
merge 1:1 id using "./data/baseline.dta" ;
tab _merge ;

* correction to the variable label ;
label var savings_bank "has bank savings account" ;

* ***************************************************************************** ;
* sample selection
* ***************************************************************************** ;

* drop households with baseline but no endline ;
drop if _merge == 2 ; 
drop _merge ;

* drop households with no financial knowledge scores ; 
egen temp_knowl = rowmean(numer_fin_return-attit_budget) ;
drop if temp_knowl == . ;
drop temp_knowl ; 

* identify respondents with endline measures of numeracy ;
merge 1:1 id using "./data/financial-knowledge-short-term", keepusing(id) ;
tab _merge ;
keep if _merge == 3 ;
drop _merge ;

* drop households with missing discount rate (control variable) ;
drop if disc_rate == . ;

* ***************************************************************************** ;
* create financial knowledge scores
* ***************************************************************************** ;

egen end_numeracy = rowmean(numer_fin_return-numer_int_rate) ;
egen end_awareness = rowmean(aware_budget-aware_unprod) ;
egen end_attitudes = rowmean(attit_suggest_ins-attit_budget) ;

* ***************************************************************************** ;
* create treatment dummies
* ***************************************************************************** ;

gen flcounsgoal = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 1)  ;
gen flcouns = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 0) ;
gen flgoal = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 1) ;
gen fl = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 0) ;
gen control = (fin_ed_treatment == 0 & couns_treatment == 0 & goal_treatment == 0) ;

* ***************************************************************************** ;
* control variables
* ***************************************************************************** ;

local cvars has_hard_time_saving interested_in_financial inconsistent disc_rate riskaverse  ;

* ***************************************************************************** ;
* APPENDIX TABLE A6: IV Regression
* ***************************************************************************** ;

est drop _all ;

* regressions, loop over all outcomes ; 
foreach y of varlist budget_tried savings_bank loan_purpose_bus_educ ins_life { ;
	
	* fin ed only ;
	if "`y'" == "budget_tried" { ;
		ivreghdfe `y' (end_awareness = fl) `cvars' if fl | control, a(strata) clu(wave_class) ; 
		est sto `y'1a ;

		ivreghdfe `y' (end_attitudes = fl) `cvars' if fl | control, a(strata) clu(wave_class) ; 
		est sto `y'1b ;
	} ;
	
	* fin ed + goal ;
	if "`y'" == "budget_tried" | "`y'" == "savings_bank" { ;
		ivreghdfe  `y' (end_awareness = flgoal) `cvars' if flgoal | control, a(strata) clu(wave_class) ; 
		est sto `y'2a ;
	
		ivreghdfe  `y' (end_attitudes = flgoal) `cvars' if flgoal | control, a(strata) clu(wave_class) ; 
		est sto `y'2b ;
	} ;
	
	* fin ed + couns ;
	ivreghdfe  `y' (end_awareness = flcouns) `cvars' if flcouns | control, a(strata) clu(wave_class) ; 
	est sto `y'3a ;

	ivreghdfe  `y' (end_attitudes = flcouns) `cvars' if flcouns | control, a(strata) clu(wave_class) ; 
	est sto `y'3b ;

	* fin ed + couns + goal ;
	if "`y'" == "budget_tried" | "`y'" == "savings_bank" | "`y'" == "ins_life" { ;
		ivreghdfe  `y' (end_awareness = flcounsgoal) `cvars' if flcounsgoal | control, a(strata) clu(wave_class) ;
		est sto `y'4a ;
	
		ivreghdfe  `y' (end_attitudes = flcounsgoal) `cvars' if flcounsgoal | control, a(strata) clu(wave_class) ;
		est sto `y'4b ;
	} ;
	
} ;

shell printf "********** Appendix Table A6 ********** \r\n" > "./output/appendix-table-A06.csv" ;

* output results ;
forvalues i = 1/4 { ;	

	esttab *`i'* using "./output/appendix-table-A06.csv", 
		append depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par)) drop(`cvars')
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f) 
		labels("Observations"))	;
		
	shell printf "\r\n" >> "./output/appendix-table-A06.csv" ;

} ;

exit ;
